/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-20 14:06:11
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-21 11:08:39
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/pv_inverter_detail/pv_inverter_detail_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/card_wrap.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/yt_table/yt_profile_table.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/devices/components/chart/device_data_history_chart.dart';
import 'package:yt_devops/pages/devices/device_tab/components/form_text_list.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/new_air_cooling_page/component/dynamic_tabview.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_util.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import '../../../../../components/log_utils.dart';
import 'pv_inverter_detail_controller.dart';
import 'pv_inverter_detail_model.dart';

class PvInverterDetailPage extends GetView<PvInverterDetailController> {
  PvInverterDetailPage({Key? key}) : super(key: key);

  @override
  PvInverterDetailController get controller =>
      Get.put<PvInverterDetailController>(PvInverterDetailController());

  @override
  Widget build(BuildContext context) {
    Log.d(' controller.model.propertyList==' +
        controller.model.propertyList.toString());
    return Scaffold(
      appBar: AppBarWidget.defaultAppBar(controller.title),
      body: GetBuilder<PvInverterDetailController>(
        dispose: (state) => Get.delete<PvInverterDetailController>(),
        builder: (_) => SafeArea(
          child: ListView(
            padding: EdgeInsets.zero,
            children: [
              CardWrap(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    DeviceUtil.headTitle(ResKey.deviceDCInput.tr,
                        padding: EdgeInsets.only(left: 16.w)), //直流输入
                    YtProfileTable<CurrentType>(
                        tableWidth: MediaQuery.of(context).size.width - 32.w,
                        dataSource: controller.inputDatas,
                        columns: PvInverterDetailModel.inputColoums,
                        isExpand: true,
                        rowsCount: 4)
                  ],
                ),
              ),
              CardWrap(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    DeviceUtil.headTitle(ResKey.deviceACOutput.tr,
                        padding: EdgeInsets.only(left: 16.w)), //交流输出
                    YtProfileTable<CurrentType>(
                      tableWidth: MediaQuery.of(context).size.width - 32.w,
                      dataSource: controller.outputDatas,
                      columns: PvInverterDetailModel.outputColoums,
                    )
                  ],
                ),
              ),
              CardWrap(
                child: dynamicTabViews(
                  controller.model.propertyList,
                  isCanScroll: false,
                ),
              ),
              if (controller.dayCapProperty != null)
                KeepAliveWrapper(CardWrap(
                    padding: EdgeInsets.only(left: 16.w),
                    child: DeviceDataHistoryChart(
                        height: 232.h,
                        deviceId: controller.deviceId,
                        amount: controller.dataSource?['dayCap'],
                        property: controller.dayCapProperty!,
                        dataItemName: 'siteMonitor.generatingCapacity'.tr))),
            ],
          ),
        ),
      ),
    );
  }

  dynamicTabViews(List list, {bool isCanScroll = false}) {
    return ListView.builder(
      shrinkWrap: true,
      physics: isCanScroll ? null : const NeverScrollableScrollPhysics(),
      padding: EdgeInsets.zero,
      itemBuilder: (BuildContext context, int index) {
        Map map = list[index];
        return _itemRowWidget(map['name'].toString(),
            map['value'].toString() == 'null' ? '-' : map['value'].toString());
      },
      itemCount: list.length,
    );
  }

  _itemRowWidget(String title, String realValue) {
    return title == 'margin'
        ? Container(
            height: 10,
            color: Colour.backgroundColor,
          )
        : Container(
            alignment: Alignment.centerLeft,
            height: 50,
            color: Colors.white,
            child: Column(
              children: [
                SizedBox(
                  height: 49,
                  child: Row(
                    children: [
                      Gaps.hGap16,
                      Expanded(
                        child: Container(
                          // width: 100,
                          child: Text(
                            title,
                            maxLines: 2,
                            style: ytTextStyle(
                                fontSize: 14, color: Colour.otherTextColor),
                          ),
                        ),
                      ),
                      Container(
                        width: 200,
                        alignment: Alignment.centerRight,
                        child: Text(
                          realValue,
                          textAlign: TextAlign.end,
                          maxLines: 2,
                          style: ytTextStyle(
                              fontSize: 14, color: Colour.mainTextColor),
                        ),
                      ),
                      Gaps.hGap16,
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 16.w, right: 16.w),
                  height: 1,
                  color: Colour.backgroundColor,
                )
              ],
            ),
          );
  }
}
